package com.zhijian.wharf.controller.web;

import com.zhijian.response.annotation.Response;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.annotation.LogOperation;
import com.zhijian.wharf.entity.po.Menu;
import com.zhijian.wharf.entity.vo.request.MenuSearchVo;
import com.zhijian.wharf.entity.vo.response.MenuByRoleVo;
import com.zhijian.wharf.entity.vo.response.MenuVo;
import com.zhijian.wharf.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单信息 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-22 15:30:23
 */
@Slf4j
@Controller
@Api(tags = "菜单")
public class MenuController {

    @Autowired
    private MenuService service;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/menu")
    @ApiOperation("新增")
    @LogOperation("新增菜单")
    public boolean insert(@RequestBody Menu param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/menu/{id}")
    @ApiOperation("删除")
    @LogOperation("删除菜单")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/menu/{id}")
    @ApiOperation("修改")
    @LogOperation("修改菜单")
    public boolean update(@PathVariable("id") Long id, @RequestBody Menu param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/menu/{id}")
    @ApiOperation("根据id查询")
    @LogOperation("查询菜单详情")
    public MenuVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/menu/page")
    @ApiOperation("分页查询")
    @LogOperation("分页查询菜单")
    public Page<MenuVo> selectByCondition(@RequestBody(required = false) MenuSearchVo param) {
        if (param == null)
            param = MenuSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 菜单树状数据
     *
     * @return
     */
    @Response
    @GetMapping("menu/tree")
    @ApiOperation("菜单树状数据")
    @LogOperation("树状数据查询菜单")
    public List<MenuVo> locationVoTreeList() {
        return service.renderTreeList();
    }

    /**
     * 根据角色id查询菜单树状数据
     *
     * @return
     */
    @Response
    @GetMapping("menu/tree/{roleId}")
    @ApiOperation("根据角色id查询菜单树状数据")
    public MenuByRoleVo locationVoTreeList(@PathVariable("roleId") Long roleId) {
        return service.selectMenuByRoleVoByRoleId(roleId);
    }

}